/*

Frequency of use: 1 [2] 3 4 5 MEDIUM LOW

Short:
    Performs staged processing using a series of different objects.

Long:
    In Object Oriented Design, the chain-of-responsibility pattern is a design 
pattern consisting of a source of command objects and a series of processing 
objects. Each processing object contains a set of logic that describes the types 
of command objects that it can handle, and how to pass off those that it cannot 
to the next processing object in the chain. A mechanism also exists for adding 
new processing objects to the end of this chain.
In a variation of the standard chain-of-responsibility model, some handlers may 
act as dispatchers, capable of sending commands out in a variety of directions, 
forming a tree of responsibility. In some cases, this can occur recursively, 
with processing objects calling higher-up processing objects with commands that 
attempt to solve some smaller part of the problem; in this case recursion 
continues until the command is processed, or the entire tree has been explored. 
An XML interpreter (parsed, but not yet executed) might be a fitting example.
This pattern promotes the idea of loose coupling, which is considered a 
programming best practice.

Information sources:
 - http://calumgrant.net/patterns/index.html
 - http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern
 - http://www.dofactory.com/Patterns/Patterns.aspx
*/

#include <string>
#include <iostream>

class Output
{
public:
	void output(const std::string & str)
	{
		std::cout << str << std::endl;
	}
};

class World
{
	Output & out;
public:
	World(Output & out) : out(out) { }
	void output(const std::string & str)
	{
		out.output(str + " world!");
	}
};

class Hello
{
	World & world;
public:
	Hello(World & world) : world(world) { }
	void output()
	{
		world.output("Hello");
	}
};

void hello_world(Hello & hello)
{
	hello.output();
}

int main()
{
	Output output;
	World world(output);
	Hello hello(world);
	hello_world(hello);
	return 0;
}

